2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_TREEVIEWHANDLER_JUCEHEADER__
27 #define __JUCER_TREEVIEWHANDLER_JUCEHEADER__
30 //==============================================================================
33 class TreeViewHandler
: public ComponentTypeHandler
36 //==============================================================================
38 : ComponentTypeHandler ("TreeView", "TreeView", typeid (DemoTreeView
), 150, 150)
40 registerColour (TreeView::backgroundColourId
, "background", "backgroundColour");
41 registerColour (TreeView::linesColourId
, "lines", "linecol");
44 //==============================================================================
45 Component
* createNewComponent (JucerDocument
*)
47 return new DemoTreeView();
50 XmlElement
* createXmlFor (Component
* comp
, const ComponentLayout
* layout
)
52 TreeView
* const t
= dynamic_cast <TreeView
*> (comp
);
53 XmlElement
* const e
= ComponentTypeHandler::createXmlFor (comp
, layout
);
55 e
->setAttribute ("rootVisible", t
->isRootItemVisible());
56 e
->setAttribute ("openByDefault", t
->areItemsOpenByDefault());
61 bool restoreFromXml (const XmlElement
& xml
, Component
* comp
, const ComponentLayout
* layout
)
63 if (! ComponentTypeHandler::restoreFromXml (xml
, comp
, layout
))
66 TreeView defaultTreeView
;
67 TreeView
* const t
= dynamic_cast <TreeView
*> (comp
);
69 t
->setRootItemVisible (xml
.getBoolAttribute ("rootVisible", defaultTreeView
.isRootItemVisible()));
70 t
->setDefaultOpenness (xml
.getBoolAttribute ("openByDefault", defaultTreeView
.areItemsOpenByDefault()));
75 void getEditableProperties (Component
* component
, JucerDocument
& document
, Array
<PropertyComponent
*>& properties
)
77 ComponentTypeHandler::getEditableProperties (component
, document
, properties
);
78 TreeView
* const t
= dynamic_cast <TreeView
*> (component
);
80 properties
.add (new TreeViewRootItemProperty (t
, document
));
81 properties
.add (new TreeViewRootOpennessProperty (t
, document
));
83 addColourProperties (t
, document
, properties
);
86 const String
getCreationParameters (Component
* comp
)
88 return quotedString (comp
->getName());
91 void fillInCreationCode (GeneratedCode
& code
, Component
* component
, const String
& memberVariableName
)
93 TreeView defaultTreeView
;
94 TreeView
* const t
= dynamic_cast <TreeView
*> (component
);
96 ComponentTypeHandler::fillInCreationCode (code
, component
, memberVariableName
);
98 if (defaultTreeView
.isRootItemVisible() != t
->isRootItemVisible())
101 << memberVariableName
<< "->setRootItemVisible ("
102 << boolToString (t
->isRootItemVisible()) << ");\n";
105 if (defaultTreeView
.areItemsOpenByDefault() != t
->areItemsOpenByDefault())
108 << memberVariableName
<< "->setDefaultOpenness ("
109 << boolToString (t
->areItemsOpenByDefault()) << ");\n";
112 code
.constructorCode
<< getColourIntialisationCode (component
, memberVariableName
);
114 code
.constructorCode
<< "\n";
118 //==============================================================================
119 class DemoTreeView
: public TreeView
123 : TreeView ("new treeview")
125 setRootItem (new DemoTreeViewItem ("Demo root node", 4));
130 TreeViewItem
* root
= getRootItem();
136 class DemoTreeViewItem
: public TreeViewItem
139 DemoTreeViewItem (const String
& name_
, const int numItems
)
142 for (int i
= 0; i
< numItems
; ++i
)
143 addSubItem (new DemoTreeViewItem ("Demo sub-node " + String (i
), numItems
- 1));
150 void paintItem (Graphics
& g
, int width
, int height
)
153 g
.fillAll (Colours::lightblue
);
155 g
.setColour (Colours::black
);
156 g
.setFont (height
* 0.7f
);
157 g
.drawText (name
, 4, 0, width
- 4, height
, Justification::centredLeft
, true);
160 bool mightContainSubItems()
169 //==============================================================================
170 class TreeViewRootItemProperty
: public ComponentBooleanProperty
<TreeView
>
173 TreeViewRootItemProperty (TreeView
* comp
, JucerDocument
& document
)
174 : ComponentBooleanProperty
<TreeView
> ("show root item", "Root item visible", "Root item visible", comp
, document
)
178 void setState (bool newState
)
180 document
.perform (new TreeviewRootChangeAction (component
, *document
.getComponentLayout(), newState
),
181 "Change TreeView root item");
184 bool getState() const
186 return component
->isRootItemVisible();
190 class TreeviewRootChangeAction
: public ComponentUndoableAction
<TreeView
>
193 TreeviewRootChangeAction (TreeView
* const comp
, ComponentLayout
& layout
, const bool newState_
)
194 : ComponentUndoableAction
<TreeView
> (comp
, layout
),
197 oldState
= comp
->isRootItemVisible();
203 getComponent()->setRootItemVisible (newState
);
211 getComponent()->setRootItemVisible (oldState
);
216 bool newState
, oldState
;
220 //==============================================================================
221 class TreeViewRootOpennessProperty
: public ComponentChoiceProperty
<TreeView
>
224 TreeViewRootOpennessProperty (TreeView
* comp
, JucerDocument
& document
)
225 : ComponentChoiceProperty
<TreeView
> ("default openness", comp
, document
)
227 choices
.add ("Items open by default");
228 choices
.add ("Items closed by default");
231 void setIndex (int newIndex
)
233 document
.perform (new TreeviewOpennessChangeAction (component
, *document
.getComponentLayout(), newIndex
== 0),
234 "Change TreeView openness");
239 return component
->areItemsOpenByDefault() ? 0 : 1;
243 class TreeviewOpennessChangeAction
: public ComponentUndoableAction
<TreeView
>
246 TreeviewOpennessChangeAction (TreeView
* const comp
, ComponentLayout
& layout
, const bool newState_
)
247 : ComponentUndoableAction
<TreeView
> (comp
, layout
),
250 oldState
= comp
->areItemsOpenByDefault();
256 getComponent()->setDefaultOpenness (newState
);
264 getComponent()->setDefaultOpenness (oldState
);
269 bool newState
, oldState
;
275 #endif // __JUCER_TREEVIEWHANDLER_JUCEHEADER__